package com.example.springapplicationdemo.exceptionhandler;

import com.example.springapplicationdemo.exceptionhandler.exception.UserDisabledException;
import com.example.springapplicationdemo.exceptionhandler.exception.UserNotFoundException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("user_page_controller")
public class UserPageController {

    /**
     * 正常处理流程中，只抛出异常，而不处理异常
     * @param id
     * @return
     */
    @GetMapping("{id}/_update")
    public ModelAndView updateUser(@PathVariable Long id){
        if (id.equals(1L)){
            throw new UserNotFoundException(id);
        }
        if (id.equals(2L)){
            throw new UserDisabledException(id);
        }
        return new ModelAndView("controller_success");
    }




    /**
     * 异常处理流程 <br />
     * Controller中的ExceptionHandler只会处理本Controller抛出的异常 <br />
     * 处理UserNotFoundException异常
     * @param e
     * @return
     */
    @ExceptionHandler
    public ModelAndView handleException(UserNotFoundException e){
        ModelAndView mv =  new ModelAndView("error");
        mv.addObject("error", e);
        return mv;
    }
}
